{
 "metadata": {
  "name": "",
  "signature": "sha256:bedd2d93552b56f12dfc316010dc889b286a3ae6dad17066f6067cfe5a3cc22c"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Labelling the class values for the twitter dataset.\n",
      "import os\n",
      "input_filename = os.path.join(os.path.expanduser(\"~\"), \"Data\", \"twitter\", \"python_tweets.json\")\n",
      "classes_filename = os.path.join(os.path.expanduser(\"~\"), \"Data\", \"twitter\", \"python_classes.json\")"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 114
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import json\n",
      "tweets = []\n",
      "with open(input_filename) as inf:\n",
      "    for line in inf:\n",
      "        if len(line.strip()) == 0:\n",
      "            continue\n",
      "        tweets.append(json.loads(line))\n",
      "print(\"Loaded {} tweets\".format(len(tweets)))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Loaded 200 tweets\n"
       ]
      }
     ],
     "prompt_number": 115
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "tweet_sample = tweets"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 116
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "labels = []\n",
      "if os.path.exists(classes_filename):\n",
      "    with open(classes_filename) as inf:\n",
      "        labels = json.load(inf)\n",
      "print(len(labels))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "0\n"
       ]
      }
     ],
     "prompt_number": 118
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "def get_next_tweet():\n",
      "    return tweet_sample[len(labels)]['text']"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 117
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "%%javascript\n",
      "\n",
      "\n",
      "function set_label(label){\n",
      "    var kernel = IPython.notebook.kernel;\n",
      "    kernel.execute(\"labels.append(\" + label + \")\");\n",
      "    load_next_tweet();\n",
      "}\n",
      "\n",
      "function load_next_tweet(){\n",
      "   var code_input = \"get_tweet()\";\n",
      "   var kernel = IPython.notebook.kernel;\n",
      "   var callbacks = { 'iopub' : {'output' : handle_output}};\n",
      "   kernel.execute(code_input, callbacks, {silent:false});\n",
      "}\n",
      "\n",
      "function handle_output(out){\n",
      "   console.log(out);\n",
      "   var res = out.content.data[\"text/plain\"];\n",
      "   $(\"div#tweet_text\").html(res);\n",
      "}\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "javascript": [
        "function handle_output(out){\n",
        "   console.log(out);\n",
        "   var res = out.content.data[\"text/plain\"];\n",
        "   $(\"div#tweet_text\").html(res);\n",
        "}\n",
        "\n",
        "function set_useful(){\n",
        "    var kernel = IPython.notebook.kernel;\n",
        "    kernel.execute(\"classifications.append(1)\");\n",
        "    load_next_tweet();\n",
        "}\n",
        "\n",
        "function set_notuseful(){\n",
        "    var kernel = IPython.notebook.kernel;\n",
        "    kernel.execute(\"classifications.append(0)\");\n",
        "    load_next_tweet();\n",
        "}\n",
        "\n",
        "function load_next_tweet(){\n",
        "   var code_input = \"get_tweet()\";\n",
        "   var kernel = IPython.notebook.kernel;\n",
        "   var callbacks = { 'iopub' : {'output' : handle_output}};\n",
        "   var msg_id = kernel.execute(code_input, callbacks, {silent:false});\n",
        "}\n"
       ],
       "metadata": {},
       "output_type": "display_data",
       "text": [
        "<IPython.core.display.Javascript at 0x10562f438>"
       ]
      }
     ],
     "prompt_number": 119
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "%%html\n",
      "<div name=\"tweetbox\">\n",
      "    Instructions: Click in text box. Enter a 1 if the tweet is relevant, enter 0 otherwise.<br>\n",
      "    Tweet: <div id=\"tweet_text\" value=\"text\"></div><br>\n",
      "    <input type=text id=\"capture\"></input><br>\n",
      "</div>\n",
      "<script>\n",
      "$(\"input#capture\").keypress(function(e) {\n",
      "    console.log(e);\n",
      "  if(e.which == 48) {\n",
      "    // 0 pressed\n",
      "    set_label(0);\n",
      "    $(\"input#capture\").val(\"\");\n",
      "  }else if (e.which == 49){\n",
      "    // 1 pressed\n",
      "    set_label(1);  \n",
      "    $(\"input#capture\").val(\"\");\n",
      "  }\n",
      "});\n",
      "\n",
      "load_next_tweet();\n",
      "</script>"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "html": [
        "<div name=\"tweetbox\">\n",
        "    Instructions: Click in text box. Enter a 1 if the tweet is relevant, enter 0 otherwise.<br>\n",
        "    Tweet: <div id=\"tweet_text\" value=\"text\"></div><br>\n",
        "    <input type=text id=\"capture\"></input><br>\n",
        "</div>\n",
        "<script>\n",
        "$(\"input#capture\").keypress(function(e) {\n",
        "    console.log(e);\n",
        "  if(e.which == 48) {\n",
        "    // 0 pressed\n",
        "    set_notuseful();\n",
        "    $(\"input#capture\").val(\"\");\n",
        "  }else if (e.which == 49){\n",
        "    // 1 pressed\n",
        "    set_useful();  \n",
        "    $(\"input#capture\").val(\"\");\n",
        "  }\n",
        "});\n",
        "\n",
        "load_next_tweet();\n",
        "</script>"
       ],
       "metadata": {},
       "output_type": "display_data",
       "text": [
        "<IPython.core.display.HTML at 0x10562fcf8>"
       ]
      }
     ],
     "prompt_number": 120
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "print(len(labels))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "200\n"
       ]
      }
     ],
     "prompt_number": 106
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "with open(classes_filename, 'w') as outf:\n",
      "    json.dump(labels, outf)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 107
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 93
    }
   ],
   "metadata": {}
  }
 ]
}